package com.lh.project.controller;


import com.lh.project.common.*;
import com.lh.project.model.entity.Bill;
import com.lh.project.model.entity.Food;
import com.lh.project.model.entity.User;
import com.lh.project.model.enums.SystemConstants;
import com.lh.project.service.BillService;
import com.lh.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/bill")
@Slf4j
public class BillController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Resource
    private BillService billService;

    @Resource
    private UserService userService;

    /**
     * 生成账单
     * @param arr
     * @param request
     * @return
     */
    @PostMapping("/post")
    public BaseResponse<String> createBill(@RequestBody List<Long> arr,HttpServletRequest request){
        //先判断该用户是否绑定了学号，如果没有绑定学号的话，要求其绑定学号
//        String userId = request.getHeader("userId");
        Long id = UserHolder.getUser().getId();
        User user = userService.getById(id);

        if(user.getStuNumber()== null || user.getStuNumber().equals("")){
            return ResultUtils.error(ErrorCode.NOT_BIND_STUNUMBER,"请绑定学号再下单");
        }
        boolean flag = billService.createBill(arr,id);
        return ResultUtils.success("账单生成成功");
    }

    /**
     * 获取用户账单
     * @return
     */
    @GetMapping("/get")
//    public BaseResponse<List<Bill>> getBillByUserId(Long id){
    public BaseResponse<List<Bill>> getBillByUserId(){
        Long id = UserHolder.getUser().getId();
        System.out.println("发送的id"+id);
        List<Bill> list = billService.getByUserId(id);
        return ResultUtils.success(list);
    }

}
